package com.sunday.scoremanager.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.entity.Order;

import java.math.RoundingMode;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.sunday.scoremanager.base.Constants.TEST_URL;

/**
 * Created by 刘涛 on 2017/10/26.
 */

public class OrderAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<Order> dataSet;
    private LayoutInflater layoutInflater;

    public OrderAdapter(Context context, List<Order> datas) {
        this.mContext = context;
        this.dataSet = datas;
        this.layoutInflater = LayoutInflater.from(mContext);

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.list_order_item, null);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ListHolder listHolder = (ListHolder) holder;

        Order item = dataSet.get(position);
        Glide.with(mContext)
                .load(item.getImage())
                .placeholder(R.mipmap.ic_default)
                .error(R.mipmap.ic_default)
                .into(listHolder.productImg);
        listHolder.productName.setText(item.getName());
        listHolder.orderMoney.setText("消费：￥" + item.getAmount().setScale(2, RoundingMode.HALF_UP));
        listHolder.shopSetName.setText(item.getProductName());
        listHolder.shopSetNum.setText("数量" + item.getNum());
        listHolder.llPro.setVisibility(TextUtils.isEmpty(item.getProductName())?View.GONE:View.VISIBLE);
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    class ListHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.product_img)
        ImageView productImg;
        @Bind(R.id.product_name)
        TextView productName;
        @Bind(R.id.order_time)
        TextView orderTime;
        @Bind(R.id.order_money)
        TextView orderMoney;
        @Bind(R.id.shop_set_name)
        TextView shopSetName;
        @Bind(R.id.shop_set_num)
        TextView shopSetNum;
        @Bind(R.id.ll_pro)
        LinearLayout llPro;
        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
